﻿using hvppleDotNet;
using HZH_Controls.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HangzhouChipProject
{
    public partial class cReataModel :FrmWithOKCancel1
    {
        public  HObject imageReduce;  //主界面绘制的区域模板
        private Dictionary<string, string> modelUidata = new Dictionary<string, string>(); //传递模板参数
        HTuple hsmartWindows;
        public   static  HTuple modelID = new HTuple();
        public cReataModel(HSmartWindowControl hSmart)
        {
            InitializeComponent();
            LoadUiDataToSetting();  //获取保存的模板数据
            DataSynchronism();  //数据同步显示
            this.hsmartWindows = hSmart.hvppleWindow;
            this.Title = "创建模板";
            this.IsShowCloseBtn = true;
            this.BackColor = Color.FromArgb(128, 128, 255);
            imageReduce=new HObject();
           
        }
        private void getUiData()
        {
            try
            {
                modelUidata["ContrastLow"] = this.ContrastLow_numericUpDown.Value.ToString();
                modelUidata["ContrastHigh"] = this.Contrasthigh_numericUpDown.Value.ToString();
                modelUidata["Smallest"] = this.Smallest_numericUpDown.Value.ToString();
                modelUidata["NumLevels"] = this.NumLevels_numericUpDown.Value.ToString();
                modelUidata["AngleStart"] = this.AngleStart_numericUpDown.Value.ToString();
                modelUidata["AngleExtent"] = this.AngleExtent_numericUpDown.Value.ToString();
                modelUidata["AngleStep"] = this.AngleStep_numericUpDown.Value.ToString();
                modelUidata["MinContrast"] = this.MinContrast_numericUpDown.Value.ToString();
                modelUidata["Metric"] = this.Metric_comboBox.Text.ToString();
            }
            catch (Exception ex)
            {

                FrmTips.ShowTipsError(this,ex.ToString());
            }
           

        }
        private void DataSynchronism()
        {
            this.ContrastLow_ucTrackBar.Value = (float)this.ContrastLow_numericUpDown.Value;
            this.Contrasthigh_ucTrackBar.Value = (float)this.Contrasthigh_numericUpDown.Value;
            this.Smallest_ucTrackBar.Value = (float)this.Smallest_numericUpDown.Value;
            this.NumLevels_ucTrackBar.Value = (float)this.NumLevels_numericUpDown.Value;
            this.AngleStart_ucTrackBar.Value = (float)this.AngleStart_numericUpDown.Value;
            this.AngleExtent_ucTrackBar.Value = (float)this.AngleExtent_numericUpDown.Value;
            this.AngleStep_ucTrackBar.Value = (float)this.AngleStep_numericUpDown.Value;
            this.AngleStep_ucTrackBar.Value = (float)this.AngleStep_numericUpDown.Value;
            this.MinContrast_ucTrackBar.Value = (float)this.MinContrast_numericUpDown.Value;
        }
        private bool SaveUiDataToSetting()
        {
            try
            {
                Properties.CreataModelUiData.Default.ContrastLow = this.ContrastLow_numericUpDown.Value.ToString();
                Properties.CreataModelUiData.Default.ContrastHigh = this.Contrasthigh_numericUpDown.Value.ToString();
                Properties.CreataModelUiData.Default.Smallest = this.Smallest_numericUpDown.Value.ToString();
                Properties.CreataModelUiData.Default.NumLevels = this.NumLevels_numericUpDown.Value.ToString();
                Properties.CreataModelUiData.Default.AngleStart = this.AngleStart_numericUpDown.Value.ToString();
                Properties.CreataModelUiData.Default.AngleExtent = this.AngleExtent_numericUpDown.Value.ToString();
                Properties.CreataModelUiData.Default.AngleStep = this.AngleStep_numericUpDown.Value.ToString();
                Properties.CreataModelUiData.Default.MinContrast = this.MinContrast_numericUpDown.Value.ToString();
                Properties.CreataModelUiData.Default.Metric = this.Metric_comboBox.Text.ToString();
                Properties.CreataModelUiData.Default.Save();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
          
        }
        private void LoadUiDataToSetting()
        {
            try
            {
                this.ContrastLow_numericUpDown.Value = Convert.ToDecimal(Properties.CreataModelUiData.Default.ContrastLow);
                this.Contrasthigh_numericUpDown.Value = Convert.ToDecimal(Properties.CreataModelUiData.Default.ContrastHigh);
                this.Smallest_numericUpDown.Value = Convert.ToDecimal(Properties.CreataModelUiData.Default.Smallest);
                this.NumLevels_numericUpDown.Value = Convert.ToDecimal(Properties.CreataModelUiData.Default.NumLevels);
                this.AngleStart_numericUpDown.Value = Convert.ToDecimal(Properties.CreataModelUiData.Default.AngleStart);
                this.AngleExtent_numericUpDown.Value = Convert.ToDecimal(Properties.CreataModelUiData.Default.AngleExtent);
                this.AngleStep_numericUpDown.Value = Convert.ToDecimal(Properties.CreataModelUiData.Default.AngleStep);
                this.MinContrast_numericUpDown.Value = Convert.ToDecimal(Properties.CreataModelUiData.Default.MinContrast);

            }
            catch (Exception ex)
            {

                FrmTips.ShowTipsError(this, "载入创建模板参数数据时："+ex.Message.ToString());
            }

        }
        /// <summary>
        /// 按钮事件
        /// </summary>
        protected override void DoEnter()
        {
            if (SaveUiDataToSetting())
            {
                FrmTips.ShowTipsSuccess(this, "模板参数保存成功");
            };
           
        }
        protected override void DoEsc()
        {
            base.DoEsc();
        }
        /// <summary>
        /// 将TrackBar的值赋值给numericUpDown
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            Metric_comboBox.SelectedIndex= 0;          
           
            ContrastLow_ucTrackBar.ValueChanged += (s, se) => { this.ContrastLow_numericUpDown.Value = Convert.ToDecimal(this.ContrastLow_ucTrackBar.Value); };
            Contrasthigh_ucTrackBar.ValueChanged += (s, se) => { this.Contrasthigh_numericUpDown.Value = Convert.ToDecimal(this.Contrasthigh_ucTrackBar.Value); };
            Smallest_ucTrackBar.ValueChanged += (s, se) => { this.Smallest_numericUpDown.Value = Convert.ToDecimal(this.Smallest_ucTrackBar.Value); };
            NumLevels_ucTrackBar.ValueChanged += (s, se) => { this.NumLevels_numericUpDown.Value = Convert.ToDecimal(this.NumLevels_ucTrackBar.Value); };
            AngleStart_ucTrackBar.ValueChanged += (s, se) => { this.AngleStart_numericUpDown.Value = Convert.ToDecimal(this.AngleStart_ucTrackBar.Value); };
            AngleExtent_ucTrackBar.ValueChanged += (s, se) => { this.AngleExtent_numericUpDown.Value = Convert.ToDecimal(this.AngleExtent_ucTrackBar.Value); };
            AngleStep_ucTrackBar.ValueChanged += (s, se) => { this.AngleStep_numericUpDown.Value = Convert.ToDecimal(this.AngleStep_ucTrackBar.Value); };
            MinContrast_ucTrackBar.ValueChanged += (s, se) => { this.MinContrast_numericUpDown.Value = Convert.ToDecimal(this.MinContrast_ucTrackBar.Value); };

        }
        /// <summary>
        /// 创建模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ucBtnExt_Createmodel_BtnClick(object sender, EventArgs e)
        {
            imageReduce = InterLayer.GetReduceImage();
            if (imageReduce==null)
            {
                FrmTips.ShowTipsSuccess(this, "未检测到图像，请检查图像是否加载或者工具是否设置");
                return;
            }
            getUiData();
            try
            {
                 modelID = PictureProcess.CreataModel(hsmartWindows,imageReduce, modelUidata);
                if (modelID != null)
                {
                    SaveUiDataToSetting();
                    FrmTips.ShowTipsSuccess(this, "模板创建成功");
                }
            }
            catch (Exception ex)
            {
               
                FrmTips.ShowTipsError(this,ex.ToString());
            }
           
        }
        private void UidataSynchronization()
        {
         }

        private void ContrastLow_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.ContrastLow_ucTrackBar.Value =(float)this.ContrastLow_numericUpDown.Value;
        }

        private void Contrasthigh_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.Contrasthigh_ucTrackBar.Value = (float)this.Contrasthigh_numericUpDown.Value;
        }
       
        private void Smallest_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.Smallest_ucTrackBar.Value = (float)this.Smallest_numericUpDown.Value;
        }

        private void NumLevels_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.NumLevels_ucTrackBar.Value = (float)this.NumLevels_numericUpDown.Value;
        }

        private void AngleStart_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.AngleStart_ucTrackBar.Value = (float)this.AngleStart_numericUpDown.Value;
        }

        private void AngleExtent_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.AngleExtent_ucTrackBar.Value = (float)this.AngleExtent_numericUpDown.Value;
        }

        private void AngleStep_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.AngleStep_ucTrackBar.Value = (float)this.AngleStep_numericUpDown.Value;
        }

        private void MinContrast_numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            this.MinContrast_ucTrackBar.Value = (float)this.MinContrast_numericUpDown.Value;
        }

        private void ucBtnExt_ReadModel_BtnClick(object sender, EventArgs e)
        {

        }

        private void ucBtnExt_SaveModel_BtnClick(object sender, EventArgs e)
        {

        }
    }
}
